package com.refuel.controller;

import com.refuel.domain.Activity;
import com.refuel.domain.Prize;
import com.refuel.domain.UserLottery;
import com.refuel.domain.ActivityDetail;
import com.refuel.request.LotteryRequest;
import com.refuel.response.BaseResponse;
import com.refuel.response.PageResponse;
import com.refuel.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhanghao
 * @description
 * @date 2024/5/10 11:38
 **/
@RestController
@RequestMapping("/activity")
@Tag(name = "活动控制器", description = "活动相关接口")
public class ActivityController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(ActivityController.class);

    /**
     * 活动抽奖接口
     * @return BaseResponse
     */
    @Operation(summary = "活动抽奖接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.POST, path = "/lottery")
    public Response lotteryDraw(@RequestBody LotteryRequest request) {
        logger.info("activity request: {}", request);
        Response response = new Response();

        return response;
    }

    /**
     * 活动详情接口
     * @return BaseResponse
     */
    @Operation(summary = "活动详情接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/detail")
    public BaseResponse<ActivityDetail> activityDetail(@RequestParam(name = "uid") String uid,
                                                       @RequestParam(name = "activityId") String activityId) {
        logger.info("activity uid: {}, activityId: {}", uid, activityId);
        BaseResponse<ActivityDetail> response = new BaseResponse<>();

        ActivityDetail detail = new ActivityDetail();
        detail.setActivity(buildActivity("1111", "抽奖活动，最高可中100元加油卡"));
        detail.setUserLottery(buildUserLottery());
        response.setData(detail);
        return response;
    }

    /**
     * 活动列表接口
     * @return BaseResponse
     */
    @Operation(summary = "活动列表接口")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "400", description = "xx参数错误")
    })
    @RequestMapping(method = RequestMethod.GET, path = "/list")
    public PageResponse<Activity> activityList(@RequestParam(name = "activityId") String activityId,
                                               @RequestParam(name = "activityName") String activityName,
                                               @RequestParam(name = "seriesNo") String seriesNo) {
        logger.info("activity activityId: {}, activityName: {}, seriesNo: {}",
                activityId, activityName, seriesNo);
        PageResponse<Activity> response = new PageResponse<>();

        response.addData(buildActivity("1111", "抽奖活动，最高可中100元加油卡"));
        response.addData(buildActivity("2222", "抽奖活动，最高可中200元加油卡"));
        response.addData(buildActivity("3333", "抽奖活动，最高可中300元加油卡"));
        response.addData(buildActivity("4444", "抽奖活动，最高可中400元加油卡"));
        response.addData(buildActivity("5555", "抽奖活动，最高可中500元加油卡"));
        response.addData(buildActivity("6666", "抽奖活动，最高可中600元加油卡"));
        response.addData(buildActivity("7777", "抽奖活动，最高可中700元加油卡"));

        return response;
    }

    private UserLottery buildUserLottery() {
        UserLottery userLottery = new UserLottery();
        userLottery.setActivityId("123456");
        userLottery.setLotteryCode("GK5232");
        userLottery.setUid("123456");
        userLottery.setWin(true);
        userLottery.setParticipate(true);

        Prize prize = new Prize();
        prize.setName("100元加油卡");
        prize.setType("一等奖");
        prize.setQuantity("1");
        userLottery.setPrize(prize);

        return userLottery;
    }

    private Activity buildActivity(String activityId, String activityName) {
        Activity activity = new Activity();
        activity.setId(activityId);
        activity.setTitle(activityName);
        activity.setPicture("http://www.example.com/activity_pic.jpg");
        activity.setType("lottery");
        activity.setSeriesNo("20240501");
        activity.setIntegral(10);
        activity.setStartTime(new Date());
        activity.setEndTime(new Date());

        List<Prize> prizes = new ArrayList<>();
        Prize prize = new Prize();
        prize.setName("100元加油卡");
        prize.setType("一等奖");
        prize.setQuantity("1");
        prizes.add(prize);
        prize = new Prize();
        prize.setName("10次免费洗车");
        prize.setType("二等奖");
        prize.setQuantity("3");
        prizes.add(prize);
        prize = new Prize();
        prize.setName("2瓶玻璃水");
        prize.setType("三等奖");
        prize.setQuantity("5");
        prizes.add(prize);
        prize = new Prize();
        prize.setName("抽纸一包");
        prize.setType("参与奖");
        prize.setQuantity("10");
        prizes.add(prize);
        activity.setPrizes(prizes);

        return activity;
    }
}
